﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using ServiceStack;
using System;
using XuanXiang.Api.Admin.Filters;
using XuanXiang.Infrastructure.Attr;
using XuanXiang.Infrastructure.Dto.Request;
using XuanXiang.Infrastructure.Dto.Request.Goods;
using XuanXiang.Infrastructure.Dto.Response.Goods;
using XuanXiang.Infrastructure.EFCore.MySql;
using XuanXiang.Service.Interface;

namespace XuanXiang.Api.Admin.Controllers
{
    [ApiGroup("Admin(管理员模块)")]
    public class GoodsController : BaseController
    {
        private readonly I_Goods_Service _goods_Service;

        public GoodsController(I_Login_Service login_Service,I_Goods_Service goods_Service) : base(login_Service)
        {
            _goods_Service = goods_Service;
        }


        /// <summary>
        /// 商品补货{id,count}
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [Right_(urls = "/goods,/goods/goodsDetails")]
        public async Task<IActionResult> AddInventory(AddInventory_dto dto) {
            var result =await _goods_Service.Add_Inventory(dto);
            return Ok(result);
        }

        /// <summary>
        /// 商品删除{id}
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Delete_Goods(Delete_Dto dto) {
            var result = await _goods_Service.Delete_Goods(dto);
            return Ok(result);
        }

        /// <summary>
        /// 修改商品上架/下架和禁用启用{id/isShelve/isBan}
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Update_Shelve(Change_ShelveOrBan_dto dto)
        {
            var result =await _goods_Service.Update_Shelve(dto);
            return Ok(result);
        }

        /// <summary>
        /// 获取商品类别
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Get_Goods_Type(int page = 1, int limit = 10,string? key="") {
            var result = await _goods_Service.Get_Goods_Type(new Fenye_Dto() { 
            page=page,
            limit=limit,
            key=key??""
            });
            return Ok(result);
        }


        /// <summary>
        /// 商品新增
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public  IActionResult Add_Goods(GoodsAdd_Dto dto) {
            var result =  _goods_Service.Add_Goods(dto);
            return Ok(result);
        }

        /// <summary>
        /// 获取所有商品列表
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Get_Goods(int page=1,int limit=10,string? key="",int? isShelve=-1,int? isBan=-1,bool isPrice=false,string? order= "ascending") 
        {
            key=key ?? "";
            var result = await _goods_Service.Get_Goods(new Fenye_Dto() { 
            page=page,
            limit=limit,
            key=key,
            isShelve=isShelve,
            isBan=isBan,
            isPrice=isPrice,
            order=order
            });
            return Ok(result);
        }

        /// <summary>
        /// 获取单个商品详情{id}
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Get_Single_Goods(string? id) {
            var result = await _goods_Service.Get_Single_Goods(id);
            return Ok(result);
        }


        /// <summary>
        /// 获取单个商品的销售数据{id}
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Get_Single_Goods_SalesData(string? id, int page = 1, int limit = 10) {
            var result = await _goods_Service.Get_Single_Goods_SalesData(new Fenye_Dto()
            {
                page = page,
                limit = limit,
               id=id
            });
            return Ok(result);
        }
    }
}
